﻿using _1142063_1142080_1142098.Enum;
using _1142063_1142080_1142098.Models;
using _1142063_1142080_1142098.Models.DTO;
using _1142063_1142080_1142098.Models.Services;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace _1142063_1142080_1142098.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
        //private static BaseModel baseModel;

        public ActionResult Index()
        {
            var baseModel = new BaseModel()
            {
                NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode()),
                NoiDenList=new List<NoiDen>(),
                GiaList=new List<DanhMuc>()
            };
            return View(baseModel);
        }

        public ActionResult TimKiemTour(BaseModel baseModel)
        {
            DateTime temp;
            var timKiemTourModel = new TimKiemTourModels() 
            {
                NoiKhoiHanhList = DanhMucService.LayDanhSach((byte)LoaiDanhMucEnum.NoiKhoiHanh.GetHashCode()),
                NoiDenList = new List<NoiDen>(),
                GiaList=new List<DanhMuc>(),
                IsTrongNuoc=byte.Parse(Request["IsTrongNuoc"]),
                NgayDi = DateTime.TryParse(Request["NgayDi"],out temp) ? DateTime.Parse(Request["NgayDi"]):(DateTime?)null,
                NoiKhoiHanh = Request["NoiKhoiHanh"],
                Gia=byte.Parse(Request["Gia"]),
                NoiDen = Request["NoiDen"],
                Trang = int.Parse(Request["Trang"])
            };
            decimal? minGia = null;
            decimal? maxGia = null;
            if (timKiemTourModel.IsTrongNuoc == (byte)0)
            {
                switch ((GiaNuocNgoaiEnum)timKiemTourModel.Gia)
                {
                    case GiaNuocNgoaiEnum.Duoi3Tr:
                        minGia = null;
                        maxGia = 3000000;
                        break;
                    case GiaNuocNgoaiEnum.BaDen7Tr:
                        minGia = 3000000;
                        maxGia = 7000000;
                        break;
                    case GiaNuocNgoaiEnum.BayDen12Tr:
                        minGia = 7000000;
                        maxGia = 12000000;
                        break;
                    case GiaNuocNgoaiEnum.HaiHaiDen32Tr:
                        minGia = 22000000;
                        maxGia = 32000000;
                        break;
                    case GiaNuocNgoaiEnum.MuoiHaiDen15Tr:
                        minGia = 12000000;
                        maxGia = 15000000;
                        break;
                    case GiaNuocNgoaiEnum.MuoiLamDen18Tr:
                        minGia = 15000000;
                        maxGia = 18000000;
                        break;
                    case GiaNuocNgoaiEnum.MuoiTamDen22Tr:
                        minGia = 18000000;
                        maxGia = 22000000;
                        break;
                    case GiaNuocNgoaiEnum.Tren32Tr:
                        minGia = 32000000;
                        maxGia = null;
                        break;
                    default:
                        minGia = null;
                        maxGia = null;
                        break;
                }
                var data = TourService.TraCuuTourNuocNgoai(timKiemTourModel.NoiKhoiHanh, timKiemTourModel.NoiDen != "--- Tất cả ---" ? timKiemTourModel.NoiDen : string.Empty, timKiemTourModel.NgayDi, minGia, maxGia, timKiemTourModel.Trang - 1, 100);
                timKiemTourModel.TourSearchList = data.Item1;
                timKiemTourModel.SoTrang = data.Item2;
            }
            else
            {
                switch ((GiaTrongNuocEnum)timKiemTourModel.Gia)
                {
                    case GiaTrongNuocEnum.BonDen6Tr:
                        minGia = 4000000;
                        maxGia = 6000000;
                        break;
                    case GiaTrongNuocEnum.Duoi1Tr:
                        minGia = null;
                        maxGia = 1000000;
                        break;
                    case GiaTrongNuocEnum.HaiDen4Tr:
                        minGia = 2000000;
                        maxGia = 4000000;
                        break;
                    case GiaTrongNuocEnum.MotDen2Tr:
                        minGia = 1000000;
                        maxGia = 2000000;
                        break;
                    case GiaTrongNuocEnum.SauDen10Tr:
                        minGia = 6000000;
                        maxGia = 10000000;
                        break;
                    case GiaTrongNuocEnum.Tren10Tr:
                        minGia = 10000000;
                        maxGia = null;
                        break;
                    default:
                        minGia = null;
                        maxGia = null;
                        break;
                }
                var data = TourService.TraCuuTourTrongNuoc(timKiemTourModel.NoiKhoiHanh, timKiemTourModel.NoiDen != "--- Tất cả ---" ? timKiemTourModel.NoiDen : string.Empty, timKiemTourModel.NgayDi, minGia, maxGia, timKiemTourModel.Trang - 1, 100);
                timKiemTourModel.TourSearchList = data.Item1;
                timKiemTourModel.SoTrang = data.Item2;
            }
            return View(timKiemTourModel);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetNoiDen(byte isTrongNuoc)
        {
            var noiDenList = NoiDenService.LayDanhSach(isTrongNuoc);
            //baseModel.NoiDenList = noiDenList;
            return Json(noiDenList,JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult GetGia(byte isTrongNuoc)
        {
            var giaList = DanhMucService.LayDanhSach(isTrongNuoc==(byte)1?(byte)LoaiDanhMucEnum.GiaTrongNuoc.GetHashCode():(byte)LoaiDanhMucEnum.GiaNuocNgoai.GetHashCode());
            //baseModel.NoiDenList = noiDenList;
            return Json(giaList, JsonRequestBehavior.AllowGet);
        }
    }
}
